package com.eco.fanliapp.job.redmoneyjob;

import com.eco.fanliapp.service.IAppUserRedRewardService;
import com.eco.fanliapp.service.IAppUserService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;

/**
 * 定时领取任务红包到支付宝，新人红包
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class updateNewRedMoneyStatus implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(updateNewRedMoneyStatus.class);

    @Autowired
    private IAppUserRedRewardService appUserRedRewardService;


    public updateNewRedMoneyStatus() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************开始执行定时清理新人失效的红包，定时领取任务红包，新人红包任务-updateNewRedMoneyStatus：executeCount=" + executeCount + "--startTime=" + startTime);

        try {
            appUserRedRewardService.updateHbStatusTaskToUser();
            log.info("更新任务红包领取定时任务执行结果{}");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("任务红包领取定时任务错误");
            log.error("首购异常"+e.getMessage());
        }

        long endTime = System.currentTimeMillis();
        log.info("***************************************结束执行定时清理新人失效的红包，定时领取任务红包，新人红包任务-updateNewRedMoneyStatus：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));
    }
}
